<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none" placeholder="请输入搜索内容" focus="true">
			</uni-search-bar>
		</view>

		<view class="search-result-list" v-if="searchList.length">
			<view class="search-result-item" v-for="item in searchList" :key="item.goods_id" @click="goDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>

		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, index) in histories" :key="index" @click="goGoodsList(item)">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSearchList
	} from '../../api/search';
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchList: [],
				historyList: []
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		},
		methods: {
			input(e) {
				// console.log(e)
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					// console.log(this.kw)
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				const res = await getSearchList({
					query: this.kw
				})
				// console.log(res)
				this.searchList = res.message

				this.saveSearchHistory()
			},
			goDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			saveSearchHistory() {
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)

				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			goGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.search-result-list {
		padding: 0 5px;

		.search-result-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			height: 40px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			margin-top: 5px;

			::v-deep .uni-tag {
				font-size: 13px;
				color: #000;
				margin-right: 5px;
				background-color: #eee;
				border-color: #eee;
			}
		}
	}
</style>
